
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: CursorStateTest2.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar.util;

import com.sleepycat.je.DatabaseEntry;

import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;

public class CursorStateTest2 extends CursorStateTest {

    @Test
    public void testWindow1() {
        final BDB bdb = getBDB();
        CloseableNotes.performAction(new VoidDatabaseAction() {
            public void executeVoid() {
                CursorState cursorState = new CursorState(bdb.db.openCursor(env.getCurrentTransaction(), null),
                  bdb.pairs[1].key, bdb.pairs[1].key);
                assertEquals(cursorState.forward(), false);
            }
        });
    }

    @Test
    public void testWindow2() {
        final BDB bdb = getBDB();
        CloseableNotes.performAction(new VoidDatabaseAction() {
            public void executeVoid() {
                CursorState cursorState = new CursorState(bdb.db.openCursor(env.getCurrentTransaction(), null),
                  bdb.pairs[1].key, bdb.pairs[1].key);
                assertEquals(cursorState.backward(), false);
            }
        });
    };

    @Test
    public void testWindow3() {
        final BDB bdb = getBDB();
        CloseableNotes.performAction(new VoidDatabaseAction() {
            public void executeVoid() {
                CursorState cursorState = new CursorState(bdb.db.openCursor(env.getCurrentTransaction(), null),
                  bdb.pairs[0].key, bdb.pairs[1].key);
                assertEquals(cursorState.forward(), true);
                assertEquals(cursorState.getCurrentKey(), getBDB().pairs[0].key);
                assertEquals(cursorState.forward(), false);
            }
        });
    }

    @Test
    public void testWindow4() {
        final BDB bdb = getBDB();
        CloseableNotes.performAction(new VoidDatabaseAction() {
            public void executeVoid() {
                CursorState cursorState = new CursorState(bdb.db.openCursor(env.getCurrentTransaction(), null),
                  bdb.pairs[0].key, bdb.pairs[1].key);
                assertEquals(cursorState.backward(), true);
                assertEquals(cursorState.getCurrentKey(), getBDB().pairs[0].key);
                assertEquals(cursorState.backward(), false);
            }
        });
    }

    @Test
    public void testWindow5() {
        final BDB bdb = getBDB();
        CloseableNotes.performAction(new VoidDatabaseAction() {
            public void executeVoid() {
                CursorState cursorState = new CursorState(bdb.db.openCursor(env.getCurrentTransaction(), null),
                  null, bdb.pairs[1].key);
                assertEquals(cursorState.forward(), true);
                assertEquals(cursorState.getCurrentKey(), getBDB().pairs[0].key);
                assertEquals(cursorState.forward(), false);
            }
        });
    }

    @Test
    public void testWindow6() {
        final BDB bdb = getBDB();
        CloseableNotes.performAction(new VoidDatabaseAction() {
            public void executeVoid() {
                CursorState cursorState = new CursorState(bdb.db.openCursor(env.getCurrentTransaction(), null),
                  bdb.pairs[1].key, null);
                assertEquals(cursorState.backward(), true);
                assertEquals(cursorState.getCurrentKey(), getBDB().pairs[3].key);
                assertEquals(cursorState.backward(), true);
                assertEquals(cursorState.getCurrentKey(), getBDB().pairs[2].key);
                assertEquals(cursorState.backward(), true);
                assertEquals(cursorState.getCurrentKey(), getBDB().pairs[1].key);
                assertEquals(cursorState.backward(), false);
            }
        });
    }

    @Test
    public void testWindow7() {
        final BDB bdb = getBDB();
        CloseableNotes.performAction(new VoidDatabaseAction() {
            public void executeVoid() {
                CursorState cursorState = new CursorState(bdb.db.openCursor(env.getCurrentTransaction(), null),
                  new DatabaseEntry(new byte[] { (byte)0x80, (byte)0x01 }),
                  new DatabaseEntry(new byte[] { (byte)0x80, (byte)0x02 }));
                assertEquals(cursorState.backward(), true);
                assertEquals(cursorState.getCurrentKey(), getBDB().pairs[3].key);
                assertEquals(cursorState.backward(), false);
            }
        });
    }

    @Override
    protected int getNumEntries(String name) {
        return 4;
    }

    @Override
    protected void createEntry(String name, int i, DatabaseEntry key, DatabaseEntry value) {
        switch (i) {
        case 0:
            key.setData(new byte[] { (byte)0x60, (byte)0x01 });
            break;
        case 1:
            key.setData(new byte[] { (byte)0x60, (byte)0x01, (byte)0x80 });
            break;
        case 2:
            key.setData(new byte[] { (byte)0x60, (byte)0x02 });
            break;
        case 3:
            key.setData(new byte[] { (byte)0x80, (byte)0x01, (byte)0x80 });
            break;
        default:
            assert false;
            break;
        }
        value.setData(new byte[] { (byte)i });
    }
}

